import { baseModule } from "@/base/module";
import {
    LazyLoader,
    RegionTeamChooser,
    TeamScoreBoard,
} from "@sapi-game/gameComponent";
import { GamePlayer, PlayerGroup } from "@sapi-game/gamePlayer";
import { CubeRegion } from "@sapi-game/gameRegion/gameRegion";
import { Game } from "@sapi-game/main";
import { DimensionIds } from "@sapi-game/utils/vanila-data";
import { DirtShopGame } from "../game";
import { Dimensions } from "@sapi-game/constants";
import { DirtShopLobbyDaemon } from "../components/lobbyDaemon";

export class DirtShopLobbyState extends baseModule.State {
    readonly host = new PlayerGroup(GamePlayer);
    readonly attacker = new PlayerGroup(GamePlayer);

    protected override onEnter(): void {
        this.addComponent(LazyLoader, {
            pos: { x: 1917, y: -61, z: 1895 },
            dimensionId: DimensionIds.Overworld,
            onLoad: this.onLoad.bind(this),
            onUnload: () => {
                this.host.clear();
                this.attacker.clear();
            },
        });
    }

    onLoad(loader: LazyLoader) {
        loader.addComponent(TeamScoreBoard, {
            scoreboardName: "DirtShop",
            displayName: "§m泥土商店",
            teams: [
                { team: this.host, prefix: "§c[防守]" },
                { team: this.attacker, prefix: "§a[进攻]" },
            ],
        });
        loader.addComponent(RegionTeamChooser, {
            config: [
                {
                    region: new CubeRegion(
                        DimensionIds.Overworld,
                        { x: 1913, y: -60, z: 1914 },
                        { x: 1911, y: -60, z: 1912 }
                    ),
                    team: this.attacker,
                    onJoin: (p) => this.onJoin(p),
                },
                {
                    region: new CubeRegion(
                        DimensionIds.Overworld,
                        { x: 1919, y: -60, z: 1914 },
                        { x: 1917, y: -60, z: 1912 }
                    ),
                    team: this.host,
                    onJoin: (p) => this.onJoin(p),
                },
            ],
        });
        loader.addComponent(DirtShopLobbyDaemon);
    }

    onJoin(player: GamePlayer) {
        const sb = this.getComponent(TeamScoreBoard);
        sb.refreshScoreBoard();
        sb.show();
    }

    canStartGame() {
        if (Game.config.config.debugMode) return true;
        return this.host.validSize >= 1 && this.attacker.validSize >= 1;
    }

    getMapIndex() {
        const power = Dimensions.Overworld.getBlock({
            x: 1915,
            y: -60,
            z: 1917,
        })?.getRedstonePower();
        if (!power || power <= 1) {
            return 0;
        }
        if (power == 8) {
            return 1;
        }
        return 0;
    }

    start() {
        if (!this.canStartGame()) return;
        const map = this.getMapIndex();
        Game.manager.startGame(DirtShopGame, {
            host: this.host,
            attacker: this.attacker,
            config: {
                mapIndex: map,
            },
        });
        this.getComponent(LazyLoader).reload();
    }
}
